package com.tangpian.sna.core.analysis.svm.pre;

import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.io.Writer;

import edu.udo.cs.wvtool.generic.output.WVTOutputFilter;
import edu.udo.cs.wvtool.main.WVTWordVector;
import edu.udo.cs.wvtool.util.WVToolException;

public class WordVectorWriter implements WVTOutputFilter {

	private final PrintWriter out;

	WordVectorWriter(Writer out) {
		super();
		this.out = new PrintWriter(new BufferedWriter(out));
	}

	@Override
	public void write(WVTWordVector wv) throws WVToolException {
		int classValue = wv.getDocumentInfo().getClassValue();
		
		double[] value = wv.getValues();
		
		if (out != null) {
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < value.length; i++) {
				if (value[i] > 0.0) {
					sb.append(" " + (i) + ":" + value[i]);
                }
			}
			if (!sb.toString().isEmpty()) {
				out.print(classValue);
				out.print(sb);
				out.println();
			}
			
		}
	}
	
	public void close() {
		out.close();
	}

}
